package com.eportal.chinapay;
import chinapay.SecureLink;


/**
 * ǩ����
 * 
 * @author guobin
 * 
 */
public class SignData {

	private String path;
	

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	/**
	 * ����̻�ID����ǩ����ݣ�����ǩ��API�õ�ǩ��(��ǩ���û����͸�CP)
	 * 
	 * @param merId
	 * @param data
	 * @return
	 */
	public String signForCP(String merId, String data) {
		chinapay.PrivateKey key = new chinapay.PrivateKey();
		chinapay.SecureLink t;
		boolean flag;
		String ChkValue2;
		if (path == null || "".equals(path)) {
			System.out.println("can't find the PrivateKey path!");
			return null;
		}
		path += "MerPrK.key";
		flag = key.buildKey(merId, 0, path);
		if (flag == false) {
			System.out.println("build key error! merId=" + merId + " path=" + path);
			return null;
		}
		t = new chinapay.SecureLink(key);
		ChkValue2 = t.Sign(data);
		return ChkValue2;
	}

	public boolean verifyForCP(String merId, String data, String chkValue) {
		chinapay.PrivateKey key = new chinapay.PrivateKey();
		boolean flag;
		// �õ�CP��Կ���·��
		if (path == null || "".equals(path)) {
			System.out.println("can't find the PubKey path!");
			return false;
		}

//		String path = "D://log/";
		path += "PgPubk.key";
		// ��ʼ������ǩ��
		flag = key.buildKey(merId, 0, path);
		if (!flag) {
			System.out.println("verifyForCP:生成公钥失败!merId=" + merId + ",path=" + path);
			return false;
		}
		SecureLink t = new chinapay.SecureLink(key);
		boolean flag1 = t.verifyAuthToken(data, chkValue); // chkValueΪChinaPayӦ�𴫻ص����
		if (!flag1) {
			//ǩ����֤������
			System.out.println("verifyForCP error!data=" + data + ",chkValue="
					+ chkValue);
			return false;
		}
		return true;
	}
}
